<?php
error_reporting(0);
$indexpath = "index.php";
require_once "data/SessionControl.php";
require_once "data/cartfunctions.php";
require_once "createcart.php";
$purchasedpath = "data/purchased.xml";

function checkTrack($albumid, $trackno){
	$purchased = false;
	
	$lol = simplexml_load_file($GLOBALS['purchasedpath']);
	
	for ($i = 0; $i < count($lol->children()); $i++){
		if($_SESSION['uname'] == $lol->account[$i]->email){
			$foundMail = true;
			$emailNo = $i;
			break;
		}
	}

	if($foundMail==true){
		for ($i = 0; $i < count($lol->account[$emailNo]->purchased->children()); $i++){
			if( ($albumid == $lol->account[$emailNo]->purchased->song[$i]->albumid) && ($trackno == $lol->account[$emailNo]->purchased->song[$i]->trackno) ){
				$found = true;
				break;
			}
		}
	}
	
	if($found==true) return 1;
	else return 0;
	
}


function checkout(){

			$purchased = new DOMDocument('1.0', 'utf-8');
			$purchased->load($GLOBALS['purchasedpath']);
			$lol = simplexml_load_file($GLOBALS['purchasedpath']);
			
			for ($i = 0; $i < count($lol->children()); $i++){
				if($_SESSION['uname'] == $lol->account[$i]->email){
					$found = true;
					break;
				}
			}
			
			if($found==false){
				$newAcct = $purchased->createElement('account','');   
				
				$newCRLF = $purchased->createTextNode("\n		");
				$newAcct->appendChild($newCRLF);
	
			
				$emailField = $purchased->createElement('email',$_SESSION['uname']);
				$newAcct->appendChild($emailField);
				$newCRLF = $purchased->createTextNode("\n		");
				$newAcct->appendChild($newCRLF);
		
				$purchasedField = $purchased->createElement('purchased','');
				$newAcct->appendChild($purchasedField);
				$newCRLF = $purchased->createTextNode("\n			");
				$purchasedField->appendChild($newCRLF);
		
				$a = count($_SESSION['songs']);
				//echo $a;
				
				for($i=0;$i<$a;$i++){
					
					$songField = $purchased->createElement('song','');
					$purchasedField->appendChild($songField);
					$newCRLF = $purchased->createTextNode("\n				");
					$songField->appendChild($newCRLF);
					
						$albumField = $purchased->createElement('albumid',$_SESSION['songs'][$i]->getAlbumID());
						$songField->appendChild($albumField);
						$newCRLF = $purchased->createTextNode("\n				");
						$songField->appendChild($newCRLF);
				
						$trackField = $purchased->createElement('trackno',$_SESSION['songs'][$i]->getTrackNo());
						$songField->appendChild($trackField);
						$newCRLF = $purchased->createTextNode("\n			");
						$songField->appendChild($newCRLF);
			
					if($i==$a-1)
						$newCRLF = $purchased->createTextNode("\n		");
					else
						$newCRLF = $purchased->createTextNode("\n			");
					$purchasedField->appendChild($newCRLF);
				}
				
				$newCRLF = $purchased->createTextNode("\n	");
				$newAcct->appendChild($newCRLF);		
			
				$purchased->appendChild($newAcct);
			
				$acct = $purchased->getElementsByTagName("accounts")->item(0);
			
				$newCRLF = $purchased->createTextNode("    ");
				$acct->appendChild($newCRLF);

				$acct->appendChild($newAcct);

				$newCRLF = $purchased->createTextNode("\n");
				$acct->appendChild($newCRLF);
			}
			
			else{
			
				$newAcct = $purchased->getElementsByTagName("account")->item(0);   
			
			//$newCRLF = $purchased->createTextNode("\n		");
			//$newAcct->appendChild($newCRLF);
				$emailField = $purchased->getElementsByTagName("email")->item(0);
				$purchasedField = $purchased->getElementsByTagName("purchased")->item(0);
				$newCRLF = $purchased->createTextNode("	");
				$purchasedField->appendChild($newCRLF);
				$a = count($_SESSION['songs']);
				//echo $a;
				
				for($i=0;$i<$a;$i++){
					$songField = $purchased->createElement('song','');
					$purchasedField->appendChild($songField);
					$newCRLF = $purchased->createTextNode("	\n				");
					$songField->appendChild($newCRLF);
					
						$albumField = $purchased->createElement('albumid',$_SESSION['songs'][$i]->getAlbumID());
						$songField->appendChild($albumField);
						$newCRLF = $purchased->createTextNode("\n				");
						$songField->appendChild($newCRLF);
				
						$trackField = $purchased->createElement('trackno',$_SESSION['songs'][$i]->getTrackNo());
						$songField->appendChild($trackField);
						$newCRLF = $purchased->createTextNode("\n			");
						$songField->appendChild($newCRLF);
			
					if($i==$a-1)
						$newCRLF = $purchased->createTextNode("\n		");
					else
						$newCRLF = $purchased->createTextNode("\n			");
					$purchasedField->appendChild($newCRLF);
				}
				
				//$newCRLF = $purchased->createTextNode("\n	");
				//$newAcct->appendChild($newCRLF);		
			
				$purchased->appendChild($newAcct);
			
				$acct = $purchased->getElementsByTagName("accounts")->item(0);
			
				$newCRLF = $purchased->createTextNode("    ");
				$acct->appendChild($newCRLF);

				$acct->appendChild($newAcct);

				$newCRLF = $purchased->createTextNode("\n");
				$acct->appendChild($newCRLF);
			}
			
			$purchased->save($GLOBALS['purchasedpath']);
	//header('location: '.$GLOBALS['indexpath']);
}

?>